创建一个带StaticMeshComponent的AActor类

MyActor.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS() //因为Actor父类就已经是blueprintable ,所以不用在括号里面声明
class FIRSTDEMO_API AMyActor : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor's properties
//构造函数
AMyActor();

//定义组件 VisibleAnywhere 任何地方可见
UPROPERTY(VisibleAnywhere,Category="My Actor Components") //任何地方都可见
UStaticMeshComponent* StaticMesh; //组件名为 StaticMesh ,组件名为后面的指针名

//EditInstanceOnly 只有在实例中才能修改
UPROPERTY(EditInstanceOnly,Category="My Actor Properties | FVector")
//物体初始化transform
FVector InitLocation;

//VisibleInstaceOnly 在实例中可见
UPROPERTY(VisibleInstanceOnly, Category = "My Actor Properties | FVector")
FVector PlacedLocation;

//EditDefaultsOnly 只能在类默认值中编辑,实例化对象中看不到这个变量存在
UPROPERTY(EditDefaultsOnly, Category = "My Actor Properties | FVector")
bool bGotoInitLocation; //ue默认 bool变量前都要加上b
//VisibleDefaultsOnly 默认可见
UPROPERTY(VisibleDefaultsOnly, Category = "My Actor Properties | FVector")
FVector WorldOrigin;
//EditAnywhere 任何地方(类,实例)都可编辑 //限制条件:限制UE中输入框中的最小值和最大值,ClampMin实际的对应UIMin,相应的
UPROPERTY(EditAnywhere, Category = "My Actor Properties | FVector", meta = (ClampMin = -5.f, ClampMax = 5.f, UIMin = -5.f, UIMax = 5.f))
FVector TickLocationOffset;

UPROPERTY(EditAnywhere, Category = "My Actor Properties | FVector")
bool bShouldMove;

UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Physics")
FVector InitForce;

UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Physics")
FVector InitTorque;

UPROPERTY(EditInstanceOnly, Category = "My Actor Properties | Physics")
bool bAccelChange;

protected:
// Called when the game starts or when spawned
//重写父类的BeginPlay()
virtual void BeginPlay() override;

public:
// Called every frame
//event tick ,from parent class override
virtual void Tick(float DeltaTime) override;
};

MyActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActor.h"
#include "Components/StaticMeshComponent.h"

// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
//如果不需要eventtick 这个函数的话就可以设为 false,增加程序的性能
PrimaryActorTick.bCanEverTick = true;

//组件初始化
//创建一个默认的子物体,后面传入的是一个模板,TEXT宏后面是一个ue封装好的一个FString类型
//模板函数,后面传入的是一个模板
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));

InitLocation = FVector(0.0f, 100.0f, 200.0f);
PlacedLocation = FVector(0.0f);
bGotoInitLocation = false;
WorldOrigin = FVector(0.0f);
TickLocationOffset = FVector(1.f);
bShouldMove = false;

InitForce = FVector(0.f);
InitTorque = FVector(0.f);
bAccelChange = false;
}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
//调用父类的beginplay()
Super::BeginPlay();
PlacedLocation = GetActorLocation();
if (bGotoInitLocation)
{
SetActorLocation(InitLocation);
}
StaticMesh->AddForce(InitForce,TEXT("NAME_None"),bAccelChange);
StaticMesh->AddTorque(InitTorque, TEXT("NAME_None"), bAccelChange);

//FMath::Lerp();
//FMath::FRandRange();
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
//调用父类的eventtick()
Super::Tick(DeltaTime);
if (bShouldMove)
{
FHitResult HitResult;
AddActorLocalOffset(TickLocationOffset,true,&HitResult);
UE_LOG(LogTemp, Warning, TEXT("X: %.f, Y: %.f, Z: %.f"), HitResult.Location.X, HitResult.Location.Y, HitResult.Location.Z);
}
}

在这里插入图片描述